/** ADXL sensor abstraction layer.
 *
 * This file is intended to provide a simple layer of abstraction above the ADXL
 * sensor connected via TWI.
*/
//##############################################################################




/** TWI master functions */
#include "LR_twimaster.h"
//##############################################################################




/** Define ADXL ADDRESS */
#define ADDRESS 0x53 //0x1D
//##############################################################################




/** Init the ADXL sensor */
signed char adxl_init( void )
{
  static char const power_ctl = 0x08;
  static char const data_format = 0x09;

  if (LR_twimaster_send( ADDRESS, 0x2D, 1, (void *) &power_ctl, 1 ) &&
      LR_twimaster_send( ADDRESS, 0x31, 1, (void *) &data_format, 1 ))
  {
    return 1;
  }
  return 0;
}
//------------------------------------------------------------------------------




/** Read the sensor values */
void adxl_read( short * x, short * y, short * z)
{
  char raw[6];
  LR_twimaster_read( ADDRESS, 0x32, 1, raw, 6 ); // TODO possible to read all?
  *x = (raw[1] << 8) | raw[0];
  *y = (raw[3] << 8) | raw[2];
  *z = (raw[5] << 8) | raw[4];
  // TODO correct byte order?
}
//##############################################################################




